package com.CST.controller;


import com.CST.common.api.Response;
import com.CST.common.api.ResultObject;
import com.CST.entity.BdDepartment;
import com.CST.service.IBdDepartmentService;
import com.CST.vo.BdDepartmentVO;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ly
 * @since 2021-09-23
 */
@Api(tags = {"科室字典"})
@RestController
@RequestMapping("/bdDepartment")

public class BdDepartmentController {

    @Autowired
    private IBdDepartmentService targetService;


    @PostMapping("/getDepartmentSource")
    @ApiOperation("获取科室,必传参数: projectId:1,SysId:[Cost,UFIDA,HIS,Salary],(Cost时可传) DeptTypeID:1:临床服务类  2:医疗技术类 3:医疗辅助类 4:行政后勤类")
    public ResultObject getDepartmentSource(@RequestBody BdDepartment entity) {
        if (entity.getProjectId().equals(0)) {
            return ResultObject.errorReturn("院区projectId值不能为空");
        }
        return ResultObject.successReturn(targetService.getDepartmentSource(entity));
    }

    @PostMapping("/getDepartmentSourceWithOrganization")
    @ApiOperation("获取科室,必传参数: projectId:1,SysId:[Cost,UFIDA,HIS,Salary],(Cost时可传) DeptTypeID:1:临床服务类  2:医疗技术类 3:医疗辅助类 4:行政后勤类")
    public ResultObject getDepartmentSourceWithOrganization(@RequestBody BdDepartment entity) {
        if (entity.getProjectId().equals(0)) {
            return ResultObject.errorReturn("院区projectId值不能为空");
        }
        return ResultObject.successReturn(targetService.getDepartmentSourceWithOrganization(entity));
    }

    @PostMapping("/getDepartmentOrganization")
    @ApiOperation("获取成本科室组织架构 必传参数:projectId:1")
    public ResultObject getDepartmentOrganization(@RequestBody BdDepartment entity) {
        if (entity.getProjectId().equals(0)) {
            return ResultObject.errorReturn("院区projectId值不能为空");
        }
        return ResultObject.successReturn(targetService.getDepartmentOrganization(entity));
    }

    @PostMapping("/getCostDepartments")
    @ApiOperation("获取成本系统父子级科室,必传参数: isParent:true:父级科室, false:子级部门,projectId : 1,isLike:true 支持模糊查询")
    public ResultObject getCostDepartments(@RequestBody BdDepartment entity) {
        if (entity.getProjectId().equals(0)) {
            return ResultObject.errorReturn("院区projectId值不能为空");
        }
        return ResultObject.successReturn(targetService.getCostDepartments(entity));
    }

    @PostMapping("/addDepartment")
    @ApiOperation("添加科室部门")
    public ResultObject addDepartment(@RequestBody BdDepartment entity) {
        if (entity.getProjectId().equals(0)) {
            return ResultObject.errorReturn("院区projectId值不能为空");
        }
        if (entity.getDeptCode().isEmpty() || entity.getDeptName().isEmpty()) {
            return ResultObject.errorReturn("部门编号/部门名称");
        }
        BdDepartment searchInfo = new BdDepartment();
        searchInfo.setDeptCode(entity.getDeptCode());
        List<BdDepartment> searchDBRecord = targetService.getDepartmentSource(searchInfo);
        if (CollectionUtils.isNotEmpty(searchDBRecord)) {
            return ResultObject.errorReturn("部门编号已存在!");
        }

        return ResultObject.resultReturn(targetService.addDepartment(entity));
    }

    @PostMapping("/modifyDepartment")
    @ApiOperation("修改科室部门")
    public ResultObject modifyDepartment(@RequestBody BdDepartment entity) {
        if (entity.getId().equals(0)) {
            return ResultObject.errorReturn("主键不能为空");
        }
        Response result = targetService.modifyDepartment(entity);
        return ResultObject.resultReturn(result);
    }

    @GetMapping("/updateRowStatusById")
    @ApiOperation("根据Id修改状态")
    public ResultObject updateRowStatusById(Integer id, String rowStatus) {
        boolean update = targetService.updateRowStatusById(id, rowStatus);
        return update ? ResultObject.successReturn("修改成功") : ResultObject.errorReturn("修改失败");
    }

    @GetMapping("/deleteById")
    @ApiOperation("删除科室")
    public ResultObject deleteById(int id) {
        boolean result = targetService.deleteById(id);
        return result ? ResultObject.successReturn("删除成功") : ResultObject.errorReturn("删除失败");
    }
    @GetMapping("/getCostDepartment")
    @ApiOperation("科室下拉框")
    public ResultObject getCostDepartment() {
        BdDepartmentVO costDepartment = targetService.getCostDepartment();
        return ResultObject.successReturn(costDepartment) ;
    }
}

